<?php

class Tb_Games extends Models {

    private $_name = "games";
    const STATUS_INIT = 0;
    const STATUS_UPDATING = 1;
    const STATUS_GOT_URL = 2;
    const STATUS_EXPIRED = -1;
    /*
     * Insert
     */
    public function insert($games) {
        $fields = 'id_tour, id_club1, id_club2, club1_scores, club2_scores, date, 
                   first_half_club1_scores, first_half_club2_scores, round, club1_name, club2_name, season';
        
        $values = ''.$games['id_tour'].',
                   '.$games['id_club1'].',
                   '.$games['id_club2'].',
                   '.$games['club1_scores'].',
                   '.$games['club2_scores'].',
                   '.$games['date'].',
                   '.$games['first_half_club1_scores'] . ',
                   '.$games['first_half_club2_scores'] .',
                   '.$games['round'].',
                   "'.mysql_escape_string($games['club1_name']).'",
                   "'.mysql_escape_string($games['club2_name']).'",
                   "'.mysql_escape_string($games['season']).'"';

        $query = 'INSERT IGNORE INTO ' . $this->_name . '(' . $fields . ')
                      VALUES(' . $values . ')';
        $this->query($query);
    }
    
    public function update($params) {
//        'mo'    =>  $matches[7][$index],
//        'mu'    =>  $matches[9][$index],
//        'mwin'    =>  $matches[10][$index],
//        'mbalance'    =>  $matches[11][$index],
//        'mlost'    =>  $matches[12][$index],
//        'url_asian' =>  $matches[13][$index],
//        'url_ou' =>  $matches[14][$index],
//        'url_europe' =>  $matches[15][$index]
        $query = 'UPDATE games
                  SET club1_rate = "'.$params['club1_rate'].'",
                      club2_rate = "'.$params['club2_rate'].'",
                      club1_mon = '.$params['club1_mon'].',
                      club2_mon = '.$params['club2_mon'].',
                      rscores   = "'.mysql_escape_string($params['rscores']).'",
                      mo   = '.$params['mo'].',
                      mu   = '.$params['mu'].',
                      mwin   = '.$params['mwin'].',
                      mblance   = '.$params['mblance'].',
                      mlost   = '.$params['mlost'].',
                      url_asian   = "'.mysql_escape_string($params['url_asian']).'",
                      url_ou   = "'.mysql_escape_string($params['url_ou']).'",
                      url_europe   = "'.mysql_escape_string($params['url_europe']).'"
                  WHERE (date = '.$params['date'].') AND (id_club1 = '.$params['id_club1'].')
                        AND (id_club2 = '.$params['id_club2'].')';        
        return $this->query($query);
    }

    public function getUrlBankers(){
        $query = 'SELECT id, id_tour, url_asian, url_ou, url_europe
                  FROM '.$this->_name.'
                  WHERE (url_asian IS NOT NULL) OR (url_ou IS NOT NULL) OR (url_europe IS NOT NULL)';
        return $this->fetchArray($query);
    }
}